﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace GetPinYin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            textBox_Chinese.Text = "蔡奕斌";
            DateTime dt = DateTime.Today;
            DateTime tm = dt.AddYears(-1);
            DateTime tt = dt.AddMonths(-12);
            DateTime dtt = dt.AddMonths(-3);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //string ss=PinYinHelperB.GetPinYin(" ");
            //string Chinese = textBox_Chinese.Text;
            //string spi=PinyinHelper.GetPinyin(Chinese);
            //spi += string.Format("-{0}", PinYinHelperA.GetPYString(Chinese));
            //spi += string.Format("-{0}", PinYinHelperB.GetPinYin(Chinese));
            //textBlock_PinYin.Text = spi;

            Dictionary<string, string> dicPinYin = new Dictionary<string, string>();
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("config.xml");
            XmlElement xRoot = xDoc.DocumentElement;
            XmlNodeList xNodeList = xRoot.SelectNodes(@"/config/node");
            if (xNodeList!=null&&xNodeList.Count>0)
            {
                foreach (XmlNode xNode in xNodeList)
                {
                    if ((xNode.NodeType==XmlNodeType.Element)&&
                        (((XmlElement)xNode).HasAttribute("name")&&((XmlElement)xNode).HasAttribute("value"))
                        )
                    {
                        dicPinYin.Add(((XmlElement)xNode).Attributes["name"].Value,((XmlElement)xNode).Attributes["value"].Value);
                    }
                }
            }

            string strChinese = textBox_Chinese.Text;
            string firstName = string.Empty;
            string strPinYin=string.Empty;

            if (strChinese.Length > 2)
            {
                for (int i = 2; i > 0;i-- )
                {
                    firstName = strChinese.Substring(0, i);
                    if (dicPinYin.ContainsKey(firstName))
                    {
                        strPinYin = dicPinYin[firstName];
                        strPinYin += PinYinHelperB.GetPinYin(strChinese.Substring(i));
                        break;
                    }
                }
            }
            else
            {
                firstName = strChinese.Substring(0, 1);
                if (dicPinYin.ContainsKey(firstName))
                {
                    strPinYin = dicPinYin[firstName];
                    strPinYin += PinYinHelperB.GetPinYin(strChinese.Substring(1));
                }
            }
            if (string.IsNullOrEmpty(strPinYin))
            {
                strPinYin = PinYinHelperB.GetPinYin(strChinese);
            }
            textBlock_PinYin.Text = strPinYin;
        }
    }
}
